;(function (win) {
    function Local(options) {
        this.geo = null;
        this.options = options;
        this.init();
    }

    Local.prototype = {
        init: function () {
            if (navigator.userAgent.search("(MQQBrowser|Edge|Quark|QQ)") !== -1) {  // 不是-1说明是 腾讯或ie
                this.geo = new qq.maps.Geolocation("Z7LBZ-AZDKG-ZMEQT-IUULT-6UBBS-XGF2Q", "softWebKey");
            } else {
                if (win.navigator.geolocation) {
                    this.geo = win.navigator.geolocation;
                    navigator.geolocation.getLocation = function (fun1, fun2, options) {
                        navigator.geolocation.getCurrentPosition(function (p) {
                            var lng = p.coords.longitude;
                            var lat = p.coords.latitude;
                            var acy = p.coords.accuracy;
                            fun1({lng: lng, lat: lat, accuracy: acy});
                        }, fun2, options);
                    };

                    if (!this.options) {
                        this.options = {maximumAge: 1, timeout: 8000};
                    }
                } else {
                    alert("你的浏览器不支持定位！请使用夸克浏览器！");
                }
            }
        },
        getPosition: function (fun) {
            this.geo.getLocation(fun, function (error) {
                switch (error) {
                    case error.PERMISSION_DENIED:
                        alert("用户拒绝对获取地理位置的请求");
                        break;
                    case error.POSITION_UNAVAILABLE:
                        alert("位置信息是不可用的");
                        break;
                    case error.TIMEOUT:
                        alert("请求用户地理位置超时");
                        break;
                    default:
                        alert("未知错误");
                        break;
                }
            }, this.options);
        }
    };

    win.Local = Local;
})(window);

function localCheck(kid, box, fun) {
    var lc = new Local();
    var $box = box[0];
    $box.insertAdjacentHTML('afterbegin', '<div id="cl" class="text-center">\n' +
        '                <img height="64" src="/static/images/loading.jpg">\n' +
        '                <p>正在验证位置信息...</p>\n' +
        '            </div>');
    lc.getPosition(function (position) {
        var nlng = position.lng;
        var nlat = position.lat;

        $.post('/location/check/' + kid, {lng: nlng, lat: nlat}, function (data) {
            box.find('#cl').hide();
            if (data.status) {
                // alert('位置验证成功！');
                fun(data, data.lc);
            } else {
                alert('ERROR: ' + data.msg);
            }
        });
    });
}
